package com.mallotec.android.timenote.data.repository;

import androidx.lifecycle.LiveData;

import com.mallotec.android.timenote.data.bean.Timenote;
import com.mallotec.android.timenote.data.dao.TimenoteDao;
import com.mallotec.android.timenote.util.AppExecutors;

import java.util.List;

/**
 * @author reborn
 * @program TimeNote
 * @description
 * @create 2020-10-28 02:08
 **/
public class TimenoteRepository {
    private static TimenoteRepository instance;
    private TimenoteDao dao;

    public TimenoteRepository(TimenoteDao dao) { this.dao = dao; }

    public static TimenoteRepository getInstance(TimenoteDao dao) {
        if (instance == null) {
            synchronized (TimenoteRepository.class) {
                if (instance == null) {
                    instance = new TimenoteRepository(dao);
                }
            }
        }
        return instance;
    }

    public LiveData<List<Timenote>> getAllTimenotes() {
        return dao.getAllTimenotes();
    }

    public LiveData<List<Timenote>> getTimenotesByEnd(boolean isEnd) {
        if (isEnd) {
            return dao.getTimenotesByIsEnd();
        } else {
            return dao.getTimenotesByNotEnd();
        }
    }

    public LiveData<Timenote> findTimenoteById(Integer timenoteId) {
        return dao.findTimenoteById(timenoteId);
    }

    // TODO 添加和删除采用 Executor
    public void insertTimenotes(List<Timenote> timenotes) {
        AppExecutors.getInstance().diskIO().execute(() -> dao.insertTimenotes(timenotes));
    }

    public void insertTimenote(Timenote timenote) {
        AppExecutors.getInstance().diskIO().execute(() -> dao.insertTimenote(timenote));
    }

    public void updateTimenote(Timenote timenote) {
        AppExecutors.getInstance().diskIO().execute(() -> dao.updateTimenote(timenote));
    }
}
